package cn.itsource.org.service.impl;

import cn.itsource.basic.exception.BusinessException;
import cn.itsource.basic.util.BaiduAuditUtils;
import cn.itsource.basic.vo.ShopVo;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.domain.ShopAuditLog;
import cn.itsource.org.mapper.EmployeeMapper;
import cn.itsource.org.mapper.ShopAuditLogMapper;
import cn.itsource.org.mapper.ShopMapper;
import cn.itsource.org.service.IEmployeeService;
import cn.itsource.org.service.IShopService;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.mockito.internal.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author mochen
 * @since 2023-01-06
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    
    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private JavaMailSender javaMailSender;
    /*
    * 店铺入驻流程
    * 1.参数校验
    *   1>基础校验
    *       非空校验
    *       百度AI校验
    *   2>业务校验
    *       店铺名是否重复
    *       注册店铺管理员是否存在且已拥有店铺
    * 2.判断管理员是否存在---->不存在则保存该admin
    * 3.设置店铺管理员的id,并保存店铺
    * 4.将店铺id设置给管理员
    * */
    @Override
    public void settlement(Shop shop) {
//  1.参数校验
//      1>基础校验
//        1)非空校验
        if (StringUtils.isBlank(shop.getName())){
            throw new BusinessException("参数不能为空");
        }



//      2>业务校验
//        店铺名是否重复
        Shop shopInDbl = shopMapper.loadByShopName(shop.getName());
        if (shopInDbl != null){
            throw new BusinessException("店铺名重复");
        }
//        注册店铺管理员是否存在且已拥有店铺
        Employee employeeInDbl = employeeMapper.loadByUserName(shop.getAdmin().getUsername());//employeeInDbl由数据库中查询得出
        if (employeeInDbl != null && employeeInDbl.getShopId() != null){
            throw new BusinessException("该管理员已拥有店铺");
        }

//        1.1>2)百度AI校验
        Boolean res = BaiduAuditUtils.TextCensor(shop.getName());
        if (!res){//不通过抛异常
            throw new BusinessException("存在违规内容");
        }


//  2.判断管理员是否存在---->不存在则保存该admin
        if (employeeInDbl == null){
//            employeeMapper.save(shop.getAdmin());//在这一步employeeInDbl中已保存该用户名
            employeeService.add(shop.getAdmin());
            employeeInDbl = shop.getAdmin();//此时employeeInDbl一定不为空
        }

//  3.设置店铺管理员的id,并保存店铺
        shop.setAdminId(employeeInDbl.getId());
        shopMapper.save(shop);
//  4.将店铺id设置给管理员
        employeeInDbl.setShopId(shop.getId());
        employeeMapper.update(employeeInDbl);
    }

    /*1.审核通过修改店铺状态从0到1
    * 2.保存审核日志
    * 3.发送审核结果邮件
    * */
    @Override
    public void pass(ShopAuditLog log) {
        //审核通过修改店铺状态从0到1
        shopMapper.updateStateByShopId(log.getShopId(),1);
        //保存审核日志
        log.setAuditId(335L);//审核人ID,先写死,做了登录后从当前登录用户获取 TODO
        log.setAuditTime(new Date());
        log.setState(1);
        shopAuditLogMapper.save(log);
        //发送审核结果邮件
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setFrom("1651314526@qq.com");
        mailMessage.setTo("1184508554@qq.com");//发送给商家,理应查询商家邮箱后发送,先写死 TODO
        mailMessage.setSubject("审核结果通知");
        mailMessage.setText("您的店铺审核已通过,请点击链接进行激活:http://localhost:8080/shop/active/"+log.getShopId());
        javaMailSender.send(mailMessage);
    }

    /*审核通过修改店铺状态从0到-1*/
    @Override
    public void reject(ShopAuditLog log) {
        //审核通过修改店铺状态从0到1
        shopMapper.rejectStateByShopId(log.getShopId(),-1);
        //保存审核日志
        log.setAuditId(335L);//审核人ID,先写死,做了登录后从当前登录用户获取 TODO
        log.setAuditTime(new Date());
        log.setState(-1);
        shopAuditLogMapper.save(log);
        //发送审核结果邮件
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setFrom("1651314526@qq.com");
        mailMessage.setTo("1184508554@qq.com");//发送给商家,理应查询商家邮箱后发送,先写死 TODO
        mailMessage.setSubject("审核结果通知");
        mailMessage.setText("<h1>审核失败</h1><br>"+"<span>失败原因："+log.getNote()+"</span><br><a href='http://127.0.0.1:8081/#/register2?id="+log.getShopId()+"'>点击修改资料</a>");
        javaMailSender.send(mailMessage);
    }

    @Override
    public List<ShopVo> loadStateStatistics() {
        return shopMapper.loadStateStatistics();
    }
}
